home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17338 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
  2. From: grantp@usa.pipeline.com(Pete Grant)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Callback Functions In C++
  5. Date: 15 Apr 1996 10:46:40 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4kt9eg$etk@news1.h1.usa.pipeline.com>
  8. References: <4kt0ol$qdt@dub-news-svc-6.compuserve.com>
  9. NNTP-Posting-Host: 38.8.120.16
  10. X-PipeUser: grantp
  11. X-PipeHub: usa.pipeline.com
  12. X-PipeGCOS: (Pete Grant)
  13. X-Newsreader: Pipeline v3.5.0
  14.  
  15. On Apr 15, 1996 08:18:26 in article <Re: Callback Functions In C++>,
  16. '100435.736@compuserve.com (David A. Mair)' wrote: 
  17.  
  18.  
  19. >andy.walsh@ukonline.co.uk wrote: 
  20. >>Can anyone tell me how I could code a callback function that works 
  21. >>inside a class and that I can pass the address of to the waveInOpen 
  22. >>function.  It would seem that a member function carries an extra 
  23. >>'this' pointer and so cannot be addressed the same as an ordinary 
  24. >>function.  Any ideas?  I also tried to use Borland's response table 
  25. >>to handle the messages but it will only respond to a 'SendMessage' 
  26. >>command and not to my low level audio commands. 
  27. >The response you received from Dmitry covers the most important part 
  28. >of the method but I thought it would be worth adding something.  If 
  29. >you use a static member function you will not have a this pointer and 
  30. >subsequently you will not be able to access any per instance member 
  31. >variables.  A static member can only make use of other static members 
  32. >unless it has a way of obtaining the value of this for a particular 
  33. >instance of the class.  You can do this by creating a static array of 
  34. >pointers to the class type, although you will need some other 
  35. >component to ensure that you can find the correct this pointer for the 
  36. >callback that has happened.  One way might be to maintain a static 
  37. >array of Window handles with index numbers that match the index 
  38. >numbers of their associated this pointer for the class to handle the 
  39. >callback for that window.  This, of course, assumes that the callbacks 
  40. >are multiple and associated with Windows. 
  41.  
  42. PMFJI also, but IMHO, it's counterproductive to try to force C++ 
  43. semantics on a straight C paradigm.  Let's call a spade a spade 
  44. and deal with it.  I other words, rather than try to make a member 
  45. function out of an ordinary callback, make it a straight function 
  46. from which you can call a member, if appropriate.  Naturally, 
  47. there are exceptions where a static member function may be the 
  48. best way, but in most instances the following is adequate and 
  49. probably easier to understand and maintain: 
  50.  
  51. Under Motif: 
  52.     XtAddCallback(...,foo, (XtPointer)&object); 
  53.  
  54. void foo(Widget, XtPointer calldata, XtPointer) 
  55.  { 
  56.     MyClass * objptr = (MyClass*)calldata; 
  57.     objptr->memberfunc(); 
  58.  } 
  59.  
  60. Under Windows: 
  61.  
  62.    Associate a window with an object: 
  63.    SetWindowLong(hWnd, GWL_USERDATA, (long)&object); 
  64.  
  65.    In window proc: 
  66.    MyClass * objptr = (MyClass*)GetWindowLong(hWnd, GWL_USERDATA); 
  67.    objptr->memberfunc(); 
  68.  
  69. -- 
  70. Pete Grant 
  71. Kalevi, Inc. 
  72. Software Engineering & development
  73.